require 'digest/sha1'
class AddPasswordEncryption < ActiveRecord::Migration
  def self.up
    add_column :users, :crypted_password,          :string, :limit => 40
    add_column :users, :salt,                      :string, :limit => 40
    
    # add to all existing users
    User.find(:all).each do |user|
      user.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{user.username}--")
      user.crypted_password = user.encrypt(password)
    end
    
    # drop old unencrypted password column
    remove_column :users, :password
  end

  def self.down
    add_column    :users, :password

    # no good way out of this one. give everyone a random password
    User.find(:all).each do |user|
      user.password = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{user.username}--")
    end

    remove_column :users, :salt
    remove_column :users, :crypted_password
  end
end
